﻿/*
Copyright (c) 2009 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System;
using TeachMe.Application.Events;

namespace TeachMe.Application
{
    public abstract class ThreadWorkerData
    {
        public event EventHandler<StatusUpdateEventArgs> StatusUpdate;
        public event EventHandler<EventArgs> ThreadExit;

        public bool Canceled { get; private set; }
        public bool Exiting { get; private set; }

        protected void InvokeThreadExit(EventArgs e)
        {
            EventHandler<EventArgs> handler = ThreadExit;
            if (handler != null)
            {
                handler(this, e);
            }
        }
        
        protected void InvokeStatusUpdate(StatusUpdateEventArgs e)
        {
            EventHandler<StatusUpdateEventArgs> handler = StatusUpdate;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        public void Cancel()
        {
            Canceled = true;
        }

        public void Exit()
        {
            Exiting = true;

            InvokeThreadExit(EventArgs.Empty);
        }

        public void UpdateStatus(string value)
        {
            InvokeStatusUpdate(new StatusUpdateEventArgs {StatusText = value});
        }
    }
}
